<template>
    <div>
              <!-- 新增/修改职位表单 -->
        <el-dialog
            :close-on-click-modal = "false"
            ref = "dialog"
            class="posClass"
            v-dialogDrag
            width="640px"
            title = "文件资料"
            :visible.sync="dialogVisible"
            :before-close = "beforeClose"
            center>
                <el-row>
                    <el-col :span="24">
                        <el-upload
                            :lock-scroll = "false"
                            multiple  = true
                            class="upload-demo"
                            ref="uploadFile"
                            action=""
                            :before-remove = "beforeRemove"
                            :on-change = "handleFileChange"
                            :auto-upload="false">
                            <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
                            <div slot="tip" class="el-upload__tip">单个文件不能超过1M!</div>
                        </el-upload>
                    </el-col>
                </el-row>
            <span slot="footer" class="dialog-footer">
                <el-button @click.stop.native = "beforeClose">取消并关闭</el-button>
                <el-button type="primary" @click="submitProAnalyFile">确 定</el-button>
            </span>
        </el-dialog>

        
    </div>
</template>

<script>
import http from '@/assets/js/http'
import _g from '@/assets/js/global'

export default {
    props:{
        dialogVisible:{
            type:Boolean,
            default:true
        },
        currRowRecord:{
            type:Object,
            default : null,
        }
    },
    mixins: [http],
    data(){

        return{
            fileList : [],//    待上传文件
            oldFileList : [],    // 已上传文件
        }
    },
    methods: {
        /* 删除文件之前的钩子 */
        beforeRemove(file,fileList){
            console.log(file)
            console.log(fileList)
            const len = this.fileList.length
            for(let i = 0;i < len;i++){
                if(this.fileList[i].name === file.name){
                    this.fileList.splice(i,1)
                }
            }
        },
        /* 文件选择改变事件 */
        handleFileChange(file, fileList){
            this.fileList = fileList.slice()
        },
        /* 关闭弹框 */
        beforeClose(str) {
            this.oldFileList = []
            this.fileList = []
            this.$refs.uploadFile.clearFiles()
            this.$emit('hideUploadFileDialog',str)
        },
        /* 上传立项分析资料 */
        submitProAnalyFile(){
            const that = this

            const proAnalyId = that.currRowRecord.id

            if(that.fileList === []){
                _g.toastMsg('warning','请选择至少1个文件')
                return
            }

            for(let item of that.fileList){
                if(item.size >= 1024 * 1024){
                    _g.toastMsg('warning',item.name + '大小不能超过1M')
                    return
                }
            }


            const formData = new FormData()

            formData.append('proAnalyId',proAnalyId)

            for(let item of that.fileList){
                formData.append('files[]',item.raw)
            }
            // 向后台传送数据

                that.$confirm('是否确认?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                center: true
            }).then(() => {
                    _g.showLoading('提交中')
                    that.apiPost('admin/task/submitProAnalyFile', formData).then((res) => {
                        _g.showLoading().close()
                        if(typeof res === 'string'){
                            res = JSON.parse(res)
                        }
                        if (res.code != 200) {
                            that.handleError(res)
                        } else {
                            if(res.data.isSuccess === 0){             //  成功
                                _g.toastMsg("success", res.message)
                                that.beforeClose('上传资料成功')
                            }else{
                                _g.toastMsg("error", res.message)
                            }
                        }
                    })
            }).catch(() => {});
        }
    },
}
</script>

<style lang="less" scoped>

</style>


